<template>
  <div class="notification-bell" @click="goToNotifications">
    <div class="bell-icon">
      <i class="iconfont icon-xiaoxi"></i>
      <span v-if="unreadCount > 0" class="notification-badge">
        {{ unreadCount > 99 ? '99+' : unreadCount }}
      </span>
    </div>
    <span class="bell-text">通知</span>
  </div>
</template>

<script>
export default {
  name: "NotificationBell",
  data() {
    return {
      unreadCount: 0,
      timer: null
    };
  },
  created() {
    this.loadUnreadCount();
    // 每30秒检查一次未读通知
    this.timer = setInterval(() => {
      this.loadUnreadCount();
    }, 30000);
  },
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer);
    }
  },
  methods: {
    async loadUnreadCount() {
      // 只有登录用户才检查通知
      if (!localStorage.getItem('user')) {
        this.unreadCount = 0;
        return;
      }
      
      try {
        const response = await this.request.get('/api/member/notification/unread/count');
        if (response.code === '200') {
          this.unreadCount = response.data;
        }
      } catch (error) {
        // 静默处理错误，不影响用户体验
        console.error('获取未读通知数量失败:', error);
      }
    },
    
    goToNotifications() {
      if (!localStorage.getItem('user')) {
        this.$router.push('/login');
        return;
      }
      this.$router.push('/notifications');
    }
  }
};
</script>

<style scoped>
.notification-bell {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 20px;
  transition: all 0.3s ease;
  color: #ffffff;
}

.notification-bell:hover {
  background: rgba(255, 255, 255, 0.1);
  transform: scale(1.05);
}

.bell-icon {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.bell-icon i {
  font-size: 20px;
  transition: all 0.3s ease;
}

.notification-bell:hover .bell-icon i {
  animation: ring 0.5s ease-in-out;
}

.notification-badge {
  position: absolute;
  top: -8px;
  right: -8px;
  background: #ff4757;
  color: white;
  border-radius: 50%;
  min-width: 18px;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: 600;
  border: 2px solid white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.bell-text {
  font-size: 14px;
  font-weight: 500;
}

@keyframes ring {
  0%, 100% { transform: rotate(0deg); }
  10%, 30%, 50%, 70%, 90% { transform: rotate(-10deg); }
  20%, 40%, 60%, 80% { transform: rotate(10deg); }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .bell-text {
    display: none;
  }
  
  .notification-bell {
    padding: 8px;
  }
}
</style> 